#include "StdAfx.h"
#include "HCircuitModel.h"
#include "HoopsMap.h"
#include "ICircuit.h"



HCircuitModel::HCircuitModel(char const * model) : HBaseModel(model)
{
	m_pHoopMap = new CHoopsMap();
	m_pSystem = new CSystem();
	CObj::m_lFileVersion = CSystem::m_lVersion;
	m_pDevice = new CDevice();
	m_pDevice->ConfigureEntId(m_pSystem);
	m_pSystem->AddDevice(m_pDevice);

	m_pDevDis  = new CDeviceDisplayObj(m_pDevice);
	m_pDevDis->SetWidth(2000);
	m_pDevDis->SetHeight(1000);
	m_pDevDis->SetTitleHeight(10);
	m_pDevDis->SetLayerId(DEFAULT_LAYER_ID);

	m_pCirCuitDis = NULL;
}


HCircuitModel::~HCircuitModel(void)
{
	if( m_pHoopMap )
	{
		delete m_pHoopMap;
		m_pHoopMap = NULL;
	}

	if( m_pSystem )
	{
		delete m_pSystem;
		m_pSystem = NULL;
		m_pDevice = NULL;
		m_pDevDis = NULL;
		m_pCirCuitDis = NULL;

	}


// 	if( m_pDevice )
// 	{
// 		delete m_pDevice;
// 		m_pDevice = NULL;
// 	}


}

void HCircuitModel::Serialize(CArchive& ar)
{
	if( ar.IsStoring() )
	{
	//	m_pSystem->Serialize(ar);
	//	m_pDevice->Serialize(ar);
		//m_pDevDis->Serialize(ar);
		if( m_pCirCuitDis )
		{
			ar<<true;
			m_pCirCuitDis->Serialize(ar);
			CICircuit *pCiruit = (CICircuit *)m_pCirCuitDis->GetEntityObj();
			pCiruit->Serialize(ar);
		}
		else
		{
			ar<<false;
		}
	}
	else
	{

		//m_pSystem->Serialize(ar);
		//m_pDevice->Serialize(ar);
		//m_pDevDis->Serialize(ar);
		bool bVal= false;
		ar>>bVal;
		if( bVal )
		{
			CICircuit *pCiruit = new CICircuit(m_pDevice);
			m_pCirCuitDis = new CICircuitDisObj(pCiruit);
			m_pCirCuitDis->Serialize(ar);
			pCiruit->Serialize(ar);
		}

	}
	
}

void HCircuitModel::RenderCircuit( CICircuitDisObj* pCircuitDisObj )
{
	if( m_pHoopMap&&pCircuitDisObj )
	{
		m_pCirCuitDis = pCircuitDisObj;
		pCircuitDisObj->Display(m_pHoopMap);
	}
}

void HCircuitModel::BuildBigDrawingVirtualEnvironment()
{

}

void HCircuitModel::EndBigDrawingVirtualEnvironment()
{
	//Clear();
}

void HCircuitModel::Clear()
{

// 	if( m_pDevDis )
// 	{
// 		delete m_pDevDis;
// 		m_pDevDis = NULL;
// 	}
// 
// 	if( m_pDevice )
// 	{
// 		delete m_pDevice;
// 		m_pDevice = NULL;
// 	}

	if( m_pSystem )
	{
		delete m_pSystem;
		m_pSystem = NULL;
	}


}

CSystem *HCircuitModel::GetCurVirtualSystem()
{
	return m_pSystem;
}
CDevice *HCircuitModel::GetCurVirtualDevice()
{
	return m_pDevice;
}

CICircuitDisObj*HCircuitModel::GetCurCirCuitDisObj()
{
	return m_pCirCuitDis;
}

CICircuit *HCircuitModel::GetCurCirCuit()
{
	if( m_pCirCuitDis )
	{
		return (CICircuit *)m_pCirCuitDis->GetEntityObj();
	}

	return NULL;
}